package com.oracle.oa.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.oracle.oa.biz.CalendarBiz;
import com.oracle.oa.biz.DepartInfoBiz;
import com.oracle.oa.biz.PreContractBiz;
import com.oracle.oa.biz.ScheduleBiz;
import com.oracle.oa.biz.impl.OperatelogBizImpl;
import com.oracle.oa.entity.Calendar;
import com.oracle.oa.entity.Departinfo;
import com.oracle.oa.entity.Precontract;
import com.oracle.oa.entity.Schedule;
import com.oracle.oa.entity.Userinfo;

public class ScheduleActoin extends ActionSupport implements SessionAware,
		ServletResponseAware, ServletRequestAware {

	private static final long serialVersionUID = 1L;

	public Map<String, Object> session;
	private Schedule schecule;
	private Schedule sch; // // 用于修改时存值

	private Long did; // 用于增加时值得注入
	private Departinfo departinfo; // 用于增加时值得注入

	private Calendar cal; // 用于增加时值得注入

	private Long uid; // 用于增加时用户的注入
	private Userinfo userinfo;

	private String url; // 路径跳转

	private String time; // 前台获取的time

	// biz
	public ScheduleBiz sbiz;
	public PreContractBiz pbiz;
	private CalendarBiz calbiz;
	private DepartInfoBiz dbiz;

	private javax.servlet.http.HttpServletResponse response;
	private javax.servlet.http.HttpServletRequest request;

	/**
	 * 增加
	 */
	public String add() {

		System.out.println("id2222=" + did);
		
		OperatelogBizImpl.add("添加", "部门日程添加");
		
		// calendar对象
		calbiz.save(cal);

		Userinfo uf = (Userinfo) session.get("userinfo");
		int ifprivate = 1;
		// departinfo对象
		departinfo = (Departinfo) dbiz.getById(Departinfo.class, did);
   
		Schedule schedule = new Schedule(departinfo, uf, cal, ifprivate);
		sbiz.save(schedule);
		
		// 构建输入流，向页面弹出删除成功确认框
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.print("<script>alert('添加成功！')</script>");
			out.print("<script>window.location.href='../admin_pg/date_yz/department.jsp"
					+ "'</script>");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}

		return null;
	}

	/**
	 * 删除
	 * 
	 * @throws Exception
	 */

	public String delete() {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");// 防止弹出的信息出现乱码
		// 以上2句代码一定要写在获取out对象之前，否则提示信息为乱码

		OperatelogBizImpl.add("删除", "部门日程删除");
		
		List<Precontract> plist = pbiz.getByProperty(Precontract.class,
				"schedule.scheduleid", schecule.getScheduleid());

		// 假设有预约人时的处理
		if (plist != null && plist.size() > 0) {
			int result = pbiz.deleteByProperty(Precontract.class, "scheduleid",
					schecule.getScheduleid());
			if (result > 0)
				System.out.println("预约人表删除成功！");
		}

		// 没有预约人的处理
		schecule = (Schedule) sbiz.getById(Schedule.class,
				schecule.getScheduleid());

		sbiz.delete(schecule);

		// 构建输入流，向页面弹出删除成功确认框
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.print("<script>alert('删除成功！')</script>");
			out.print("<script>window.location.href='../admin_cg/date_yz/department.jsp"
					+ "'</script>");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}

		return null;
	}

	/**
	 * 修改
	 */
	public String update() {
		sbiz.update(sch);
		return SUCCESS;
	}

	/**
	 * 查找所有
	 */
	public String findAll() {
		List<Schedule> list = sbiz.getAll(Schedule.class);

		return SUCCESS;
	}

	/**
	 * 根据id查找单个信息
	 */
	public String findOne() {
		Schedule sche = (Schedule) sbiz.getById(Schedule.class,schecule.getScheduleid());
		System.out.println("SCHEDULE=" + sche.getUserinfo().getUsername());
		session.put("sche", sche);
		
		// 构建输入流，向页面弹出删除成功确认框
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.print("<script>window.location.href='../admin_cg/date_yz/department_show.jsp"
					+ "'</script>");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
		
		return null;
	}

	/**
	 * 日程的模糊查询
	 * 
	 * @return
	 */
	public String likeFind() {

		List<Schedule> clist1 = new ArrayList<Schedule>(); // 申请一个list对象

		List<Schedule> sche = new ArrayList<Schedule>();

		Long Roleid=(Long) session.get("Roleid");
		
		long id = 0l;
		long userid = 0l;

		if (schecule.getDepartinfo() == null) {
			id = 0l;
		} else {
			id = schecule.getDepartinfo().getDepartid();
		}
		if (schecule.getUserinfo() == null) {
			userid = 0l;
		} else {
			userid = schecule.getUserinfo().getUserid();
		}

		if (time.equals(null)) {
			time = "";
		}

		System.out.println("time=" + time);

		// 根据时间在calender中查找满足条件的list对象
		List<Calendar> cl = calbiz.findBytiem("start1", "%" + time + "%");

		int size = cl.size();

		System.out.println("模糊查询list对象的大小=" + size);

		// 循环满足条件的calender对象
		for (int i = 0; i < size; i++) {
			
			long ccid = cl.get(i).getId();      // 获取每个calender对象的id

			// 很据获取的日程ccid ,部门id，用户userid，查找满足条件的对象
			sche = sbiz.findByDCC(id, ccid, userid);

			for (int j = 0; j < sche.size(); j++) {

				Schedule ss = sche.get(j);

				clist1.add(ss);
			}

		}

		System.out.println("clist1=" + clist1.size());
		
		session.put("scheList", clist1);

		if(clist1.size()>0){
		
		   return  "schedupage";
		
		}else{
			
			if(1l==Roleid){
				return "cg";
			}
			if(2l==Roleid){
				return "pg";
				
			}else 
		    	return "pt";
			
		}
		
	}

	public ScheduleBiz getSbiz() {
		return sbiz;
	}

	public void setSbiz(ScheduleBiz sbiz) {
		this.sbiz = sbiz;
	}

	public PreContractBiz getPbiz() {
		return pbiz;
	}

	public void setPbiz(PreContractBiz pbiz) {
		this.pbiz = pbiz;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Schedule getSch() {
		return sch;
	}

	public void setSch(Schedule sch) {
		this.sch = sch;
	}

	public Schedule getSchecule() {
		return schecule;
	}

	public void setSchecule(Schedule schecule) {
		this.schecule = schecule;
	}

	public CalendarBiz getCalbiz() {
		return calbiz;
	}

	public void setCalbiz(CalendarBiz calbiz) {
		this.calbiz = calbiz;
	}

	public javax.servlet.http.HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(javax.servlet.http.HttpServletResponse response) {
		this.response = response;
	}

	public javax.servlet.http.HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(javax.servlet.http.HttpServletRequest request) {
		this.request = request;
	}

	public void setSession(Map<String, Object> arg0) {

		this.session = arg0;
	}

	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0;

	}

	public void setServletRequest(HttpServletRequest arg0) {

		this.request = arg0;
	}

	public void setDbiz(DepartInfoBiz dbiz) {
		this.dbiz = dbiz;
	}

	public DepartInfoBiz getDbiz() {
		return dbiz;
	}

	public Calendar getCal() {
		return cal;
	}

	public void setCal(Calendar cal) {
		this.cal = cal;
	}

	public Departinfo getDepartinfo() {
		return departinfo;
	}

	public void setDepartinfo(Departinfo departinfo) {
		this.departinfo = departinfo;
	}

	public Userinfo getUserinfo() {
		return userinfo;
	}

	public void setUserinfo(Userinfo userinfo) {
		this.userinfo = userinfo;
	}

	public Long getDid() {
		return did;
	}

	public void setDid(Long did) {
		this.did = did;
	}

	public Long getUid() {
		return uid;
	}

	public void setUid(Long uid) {
		this.uid = uid;
	}

}
